Deep Learning to find Orchards in the Jijuktu'kwejk River Watershed

In [1]:
#######################################################################################################################
# Script Name: Orchards_Detection_Attempt8.ipynb
# Purpose: To complete the entire object detection workflow in one script
# Modifications to code and comments by: Shannon England
#
# Note: the original script was obtained from the arcgis-python-api-master folder,downloaded from the website
# https://github.com/Esri/arcgis-python-api
#######################################################################################################################
In [2]:
# showing the path to the imagery used to create the training samples
# this was done using the Export Trianing Samples for Deep Learning tool in ArcGIS Pro

mosaic = r'E:\ShannonE\GDA_Project_SE\Historical_Orthomosaic\Rotated_Mosaic.tif' 
mosaic
Out[2]:
'E:\\ShannonE\\GDA_Project_SE\\Historical_Orthomosaic\\Rotated_Mosaic.tif'
In [3]:
# connecting to folder containing exported training samples : settings were 750 x 750 pixels with 375 stride in x and y direction
training_data = r'E:\ShannonE\GDA_Project_SE\Historical_Orthomosaic\Attempt8'
training_data
Out[3]:
'E:\\ShannonE\\GDA_Project_SE\\Historical_Orthomosaic\\Attempt8'

Prepare Data

In [4]:
# importing prepare data library from arcgis to apply transformations and enhance the orchards layer
from arcgis.learn import prepare_data

data = prepare_data(training_data, {1: 'Young Orchard',
                                    2: 'Mixed Orchard',
                                    3: 'Overexposed'})
In [5]:
data.show_batch()

Train SingleShotDetector Model

In [6]:
# importing the object detection model that will be used
from arcgis.learn import SingleShotDetector

# settings for single shot detector
ssd = SingleShotDetector(data, grids=[2,1])
In [7]:
# finding the optimum learning rate - low learning rate leads to slow training of model
ssd.lr_find()
In [8]:
# showing the model learning the task by showing the loss and validation for the training data
ssd.fit(10, slice(0.001, 0.02))
epoch train_loss valid_loss
1 235.774414 754.974915
2 156.127518 237.669861
3 131.243011 101.054451
4 113.051331 85.553215
5 102.591904 79.696640
6 93.387970 75.855988
7 90.120064 66.342606
8 86.701965 72.211411
9 82.396492 69.428154
10 78.237892 71.271477
C:\Program Files\ArcGIS\Pro\bin\Python\envs\arcpro_clone\lib\site-packages\torch\nn\_reduction.py:49: UserWarning: size_average and reduce args will be deprecated, please use reduction='sum' instead.
  warnings.warn(warning.format(ret))
In [13]:
# visualizing the results of the trained model
ssd.show_results(rows=20, thresh=0.05)
In [16]:
# saving the trained model
ssd.save('OrchardDetector_8')
Created model files at E:\ShannonE\GDA_Project_SE\Historical_Orthomosaic\Attempt8\models\OrchardDetector_8